package com.yjq.programmer.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yjq.programmer.bean.CodeMsg;
import com.yjq.programmer.dao.AlbumCategoryMapper;
import com.yjq.programmer.domain.AlbumCategory;
import com.yjq.programmer.domain.AlbumCategoryExample;
import com.yjq.programmer.dto.AlbumCategoryDTO;
import com.yjq.programmer.dto.PageDTO;
import com.yjq.programmer.dto.ResponseDTO;
import com.yjq.programmer.service.IAlbumCategoryService;
import com.yjq.programmer.utils.CommonUtil;
import com.yjq.programmer.utils.CopyUtil;
import com.yjq.programmer.utils.UuidUtil;
import com.yjq.programmer.utils.ValidateEntityUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 杨杨吖
 * @QQ 823208782
 * @WX yjqi12345678
 * @create 2022-11-14 9:20
 */
@Service
@Transactional
public class AlbumCategoryServiceImpl implements IAlbumCategoryService {

    @Resource
    private AlbumCategoryMapper albumCategoryMapper;

    /**
     * 分页获取专辑分类数据
     * @param pageDTO
     * @return
     */
    @Override
    public ResponseDTO<PageDTO<AlbumCategoryDTO>> getAlbumCategoryListByPage(PageDTO<AlbumCategoryDTO> pageDTO) {
        AlbumCategoryExample albumCategoryExample = new AlbumCategoryExample();
        // 判断是否进行关键字搜索
        AlbumCategoryDTO searchAlbumCategoryDTO = pageDTO.getSearchEntity();
        AlbumCategoryExample.Criteria criteria = albumCategoryExample.createCriteria();
        if(!CommonUtil.isEmpty(searchAlbumCategoryDTO.getName())) {
            criteria.andNameLike("%" + searchAlbumCategoryDTO.getName() + "%");
        }
        // 不知道当前页多少，默认为第一页
        if(pageDTO.getPage() == null){
            pageDTO.setPage(1);
        }
        if(pageDTO.getSize() == null) {
            pageDTO.setSize(5);
        }
        albumCategoryExample.setOrderByClause("sort desc, id asc");
        PageHelper.startPage(pageDTO.getPage(), pageDTO.getSize());
        // 分页查出专辑分类数据
        List<AlbumCategory> albumCategoryList = albumCategoryMapper.selectByExample(albumCategoryExample);
        PageInfo<AlbumCategory> pageInfo = new PageInfo<>(albumCategoryList);
        // 获取数据的总数
        pageDTO.setTotal(pageInfo.getTotal());
        // 将domain类型数据  转成 DTO类型数据
        List<AlbumCategoryDTO> albumCategoryDTOList = CopyUtil.copyList(albumCategoryList, AlbumCategoryDTO.class);
        pageDTO.setList(albumCategoryDTOList);
        return ResponseDTO.success(pageDTO);
    }

    /**
     * 保存专辑分类数据(添加、修改)
     * @param albumCategoryDTO
     * @return
     */
    @Override
    public ResponseDTO<Boolean> saveAlbumCategory(AlbumCategoryDTO albumCategoryDTO) {
        // 进行统一表单验证
        CodeMsg validate = ValidateEntityUtil.validate(albumCategoryDTO);
        if(!validate.getCode().equals(CodeMsg.SUCCESS.getCode())){
            return ResponseDTO.errorByMsg(validate);
        }
        AlbumCategory albumCategory = CopyUtil.copy(albumCategoryDTO, AlbumCategory.class);
        if(CommonUtil.isEmpty(albumCategory.getId())){
            // id为空 说明是添加数据
            // 判断专辑分类是否存在
            if(isAlbumCategoryExist(albumCategory, "")){
                return ResponseDTO.errorByMsg(CodeMsg.ALBUM_CATEGORY_EXIST);
            }
            // 生成8位id
            albumCategory.setId(UuidUtil.getShortUuid());
            // 添加数据到数据库
            if(albumCategoryMapper.insertSelective(albumCategory) == 0){
                return ResponseDTO.errorByMsg(CodeMsg.ALBUM_CATEGORY_ADD_ERROR);
            }
        }else{
            // id不为空 说明是修改数据
            // 判断专辑分类是否存在
            if(isAlbumCategoryExist(albumCategory, albumCategory.getId())){
                return ResponseDTO.errorByMsg(CodeMsg.ALBUM_CATEGORY_EXIST);
            }
            // 修改数据库中数据
            if(albumCategoryMapper.updateByPrimaryKeySelective(albumCategory) == 0){
                return ResponseDTO.errorByMsg(CodeMsg.ALBUM_CATEGORY_EDIT_ERROR);
            }
        }
        return ResponseDTO.successByMsg(true, "保存成功！");
    }

    /**
     * 删除专辑分类数据
     * @param albumCategoryDTO
     * @return
     */
    @Override
    public ResponseDTO<Boolean> removeAlbumCategory(AlbumCategoryDTO albumCategoryDTO) {
        if(CommonUtil.isEmpty(albumCategoryDTO.getId())){
            return ResponseDTO.errorByMsg(CodeMsg.DATA_ERROR);
        }
        // 删除专辑分类信息
        if(albumCategoryMapper.deleteByPrimaryKey(albumCategoryDTO.getId()) == 0){
            return ResponseDTO.errorByMsg(CodeMsg.ALBUM_CATEGORY_DELETE_ERROR);
        }
        return ResponseDTO.successByMsg(true, "删除成功！");
    }

    /**
     * 获取全部专辑分类数据
     * @returnux
     */
    @Override
    public ResponseDTO<List<AlbumCategoryDTO>> getAllAlbumCategory() {
        AlbumCategoryExample albumCategoryExample = new AlbumCategoryExample();
        List<AlbumCategory> albumCategoryList = albumCategoryMapper.selectByExample(albumCategoryExample);
        return ResponseDTO.success(CopyUtil.copyList(albumCategoryList, AlbumCategoryDTO.class));
    }

    /**
     * 判断专辑分类是否存在
     * @param albumCategory
     * @param id
     * @return
     */
    public Boolean isAlbumCategoryExist(AlbumCategory albumCategory, String id) {
        AlbumCategoryExample albumCategoryExample = new AlbumCategoryExample();
        albumCategoryExample.createCriteria().andNameEqualTo(albumCategory.getName());
        List<AlbumCategory> selectedAlbumCategoryList = albumCategoryMapper.selectByExample(albumCategoryExample);
        if(selectedAlbumCategoryList != null && selectedAlbumCategoryList.size() > 0) {
            if(selectedAlbumCategoryList.size() > 1){
                return true; //出现重名
            }
            if(!selectedAlbumCategoryList.get(0).getId().equals(id)) {
                return true; //出现重名
            }
        }
        return false;//没有重名
    }
}
